#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/7/7 14:16
# @Author  : CoderCharm
# @File    : auth.py
# @Software: PyCharm
# @Desc    :
from sqlalchemy import Column, Integer, String, ForeignKey,Text
"""

用户模块

"""
from datetime import datetime

from sqlalchemy import Boolean, Column, Integer, String, VARCHAR, BIGINT, SmallInteger, DateTime
from api.db.base_class import Base, gen_uuid


class AdminUser(Base):
    """
    管理员表
    """
    __tablename__ = "admin_user"
    user_id = Column(VARCHAR(32), default=gen_uuid, unique=True, comment="用户id")
    email = Column(VARCHAR(128), unique=True, index=True, nullable=False, comment="邮箱")
    phone = Column(VARCHAR(16), unique=True, index=True, nullable=True, comment="手机号")
    nickname = Column(VARCHAR(128), comment="管理员昵称")
    avatar = Column(VARCHAR(256), comment="管理员头像")
    hashed_password = Column(VARCHAR(128), nullable=False, comment="密码")
    is_active = Column(Integer, default=False, comment="邮箱是否激活 0=未激活 1=激活", server_default="0")
    role_id = Column(Integer, comment="角色表")
    __table_args__ = ({'comment': '管理员表'})


class AdminRole(Base):
    """
    简单的用户角色表设计
    """
    __tablename__ = "admin_role"
    role_id = Column(Integer, primary_key=True, index=True, comment="角色Id")
    role_name = Column(VARCHAR(64), comment="角色名字")
    permission_id = Column(BIGINT, comment="权限ID")
    re_mark = Column(VARCHAR(128), comment="备注信息")
    __table_args__ = ({'comment': '管理员角色'})


class MallUser(Base):
    """
    用户表
    """
    __tablename__ = "mall_user"
    user_id = Column(VARCHAR(32), default=gen_uuid, index=True, unique=True, comment="用户id")
    nickname = Column(VARCHAR(128), comment="用户昵称(显示用可更改)")
    username = Column(VARCHAR(128), comment="用户名(不可更改)")
    avatar = Column(VARCHAR(256), nullable=True, comment="用户头像")
    hashed_password = Column(VARCHAR(128), nullable=False, comment="密码")
    phone = Column(VARCHAR(16), unique=True, index=True, nullable=True, comment="手机号")
    gender = Column(SmallInteger, default=0, comment="性别 0=未知 1=男 2=女", server_default="0")
    register_time = Column(DateTime, default=datetime.now, comment="注册事件")
    last_login_time = Column(DateTime, default=datetime.now, comment="上次登录时间")
    last_login_ip = Column(VARCHAR(64), nullable=True, comment="上次登录IP")
    register_ip = Column(VARCHAR(64), nullable=True, comment="注册IP")
    weixin_openid = Column(VARCHAR(64), nullable=True, comment="微信openId")
    country = Column(VARCHAR(64), nullable=True, comment="国家")
    province = Column(VARCHAR(64), nullable=True, comment="省")
    city = Column(VARCHAR(64), nullable=True, comment="市")
    __table_args__ = ({'comment': '用户表'})


class MallAddress(Base):
    """
    用户地址列表
    """
    name = Column(VARCHAR(64), comment="用户昵称")
    user_id = Column(VARCHAR(32), comment="用户id")
    country_id = Column(Integer, comment="国家Id")
    province_id = Column(Integer, comment="省id")
    city_id = Column(Integer, comment="市id")
    district_id = Column(Integer, comment="区id")
    address = Column(VARCHAR(128), comment="详细地址")
    phone = Column(VARCHAR(64), comment="手机号")
    is_default = Column(SmallInteger, default=0, comment="是否默认地址", server_default="0")
    __table_args__ = ({'comment': '地址表'})


class MallSearchHistory(Base):
    """
    搜索记录
    """
    keyword = Column(VARCHAR(64), comment="搜索关键词")
    search_origin = Column(SmallInteger, default=1, comment="搜索来源 1=小程序 2=APP 3=PC", server_default="1")
    user_id = Column(VARCHAR(32), index=True, comment="用户id")
    __table_args__ = ({'comment': '搜索记录'})


class MallSiteNotice(Base):
    """
    站点消息
    """
    enabled = Column(SmallInteger, default=1, comment="是否开启 0=为开启 1=开启", server_default="1")
    content = Column(VARCHAR(256), comment="全局消息通知")
    start_time = Column(DateTime, comment="开始时间")
    end_time = Column(DateTime, comment="结束时间")

    __table_args__ = ({'comment': '站点消息'})


# class AdUser(Base):
#     """
#     管理员表
#     """
#     __tablename__ = "ad_user"
#     # user_id = Column(VARCHAR(32), default=gen_uuid, unique=True, comment="用户id")
#     email = Column(VARCHAR(128), unique=True, index=True, nullable=False, comment="邮箱")
   
#     __table_args__ = ({'comment': '后台用户表表'})



class Role(Base):
    """
    角色表
    """

    __tablename__ = "role"

    name = Column(String(128), doc='姓名')
    pid = Column(Integer, doc='父id')
    roletype = Column(Integer, doc='类型（1用户角色 2继承角色）',default=1)
    promition = Column(Integer, comment="二进制权限")
   
   
    def __str__(self):
        return self.name

class Users(Base):
    """
    用户表
    """
    __tablename__ = "users"
    account = Column(VARCHAR(32), unique=True, comment="用户账号")
    password = Column(VARCHAR(32),comment="用户密码")
    name = Column(VARCHAR(32), unique=True, comment="用户姓名")
    sex = Column(Integer, comment="0 男;1 女")
    number = Column(Integer,comment="工号")
    phone = Column(VARCHAR(11), unique=True, comment="手机号")
    company_id = Column(Integer,default=0, comment="所属公司")
    depart_id = Column(Integer,default=0, comment="所属部门")
    pid = Column(Integer, comment="职务")
    jid = Column(Integer, comment="岗位")
    rid = Column(Integer,ForeignKey('role.id'),comment="角色")

    __table_args__ = ({'comment': '用户表'})



class Resource(Base):
    """
    资源表
    """

    __tablename__ = "resource"

  
    name = Column(String(128), doc='姓名')
    pid = Column(Integer, comment="资源类型",default=0) 
    url = Column(VARCHAR(100), comment="资源路由",default='')
    restype = Column(Integer, comment="资源类型（1接口权限 2页面）")
    promition = Column(Integer, comment="二进制权限")

    __table_args__ = ({'comment': '资源表'})

    # students = db.relationship('Student', backref='teacher', secondary='student_teacher', doc='学生')

    def __str__(self):
        return self.name


class RoleResource(Base):
    """
    角色资源表
    """

    __tablename__ = "role_resource"

    id = Column(Integer, primary_key=True, doc='id')
    roleid = Column(Integer, ForeignKey("role.id"), primary_key=True, doc='角色')
    resourceid = Column(Integer, ForeignKey("resource.id"), primary_key=True, doc='资源')

    def __str__(self):
        return '{}-{}'.format(self.roleid, self.resourceid)


# 工作流标
class WorkflowAudit(Base):
    wid = Column(Integer, comment="工作流id")
    min = Column(Integer, comment="最小")
    max = Column(Integer, comment="最大")
    auditlist = Column(VARCHAR(64), comment="角色id")
    __table_args__ = ({'comment': '工作流表'})
    

# 工作流标
class Workflow(Base):
    name = Column(VARCHAR(64), comment="工作名称")
    parmars = Column(Text, comment="属性")
    status = Column(Integer, comment="状态") # 1新建  2成功  3失败
    reason = Column(VARCHAR(200), comment="理由")
    __table_args__ = ({'comment': '工作流表'})
        

# 工作流权限表
class WorkflowPermissions(Base):
    wid = Column(Integer, comment="工作流id")
    name = Column(VARCHAR(64), comment="工作名称")
    tid = Column(Integer, comment="角色id")
    __table_args__ = ({'comment': '工作流权限表'})



class Task(Base):
    wid = Column(Integer, comment="工作流id")
    uid = Column(Integer, comment="用户id")
    message = Column(VARCHAR(64), comment="内容")
    examine = Column(VARCHAR(64), comment="审批人列表")
    approve = Column(VARCHAR(64), comment="下个审批人")
    stete = Column(Integer, comment="状态")


class Examine(Base):
    tid = Column(Integer, comment="工作流id")
    aid = Column(Integer, comment="任务id")
    senduser = Column(Integer,comment="任务")
    auoituser = Column(Integer,comment="审核人")
    state = Column(Integer,comment="审计状态")
    __table_args__ = ({'comment': '审批记录表时间'})


class Company(Base):
    """
    公司表
    """
    __tablename__ = "ad_company"
    name = Column(VARCHAR(100), comment="公司名称")
    coding = Column(Integer, comment="公司编码")
    type = Column(Integer, comment="1 公司;2 部门")
    sort = Column(Integer, comment="排序")
    pid = Column(Integer,comment="父id")
    __table_args__ = ({'comment': '公司表'})




class Position(Base):
    """
    职务表
    """
    __tablename__ = "ad_duty"
    duty_name = Column(VARCHAR(32), comment="职务名称")
    duty_num = Column(Integer, comment="职务编码")
    sort = Column(Integer, comment="排序")
    descripe = Column(VARCHAR(100), comment="职务描述")
    level = Column(Integer, comment="级别")

    __table_args__ = ({'comment': '职务表'})

class Station(Base):
    """
    岗位表
    """
    __tablename__ = "ad_post"
    name = Column(VARCHAR(32), comment="岗位名称")
    coding = Column(Integer,comment="岗位编码")
    sort = Column(Integer, comment="排序")
    desc = Column(VARCHAR(80),comment="描述")

    __table_args__ = ({'comment': '岗位表'})
